package cn.dgut.training.dto.user;

import cn.dgut.training.dto.BaseDTO;
import cn.dgut.training.dto.transaction.OrdersDTO;
import cn.dgut.training.entity.transaction.Orders;
import cn.dgut.training.entity.user.Collect;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CollectDTO extends BaseDTO {

    @ApiModelProperty(value = "收藏表的ID")
    private Long id;

    @ApiModelProperty(value = "用户表的用户ID")
    private Long userId;

    @ApiModelProperty(value = "如果type=0，则是商品ID；如果type=1，则是专题ID")
    private Long valueId;

    @ApiModelProperty(value = "收藏类型，如果type=0，则是商品ID；如果type=1，则是专题ID")
    private Integer collectType;


    public static QueryWrapper<Collect> getWrapper(CollectDTO collectDTO) {

        if (collectDTO.getOrderBy() == null) {
            collectDTO.setSort("desc");
        }
        if (collectDTO.getSort() == null) {
            collectDTO.setSort("add_time");
        }

        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();

        if (collectDTO.getUserId() != null) {
            queryWrapper.eq("user_id", collectDTO.getUserId());
        }
        if (collectDTO.getCollectType() != null) {
            queryWrapper.eq("collect_type", collectDTO.getUserId());

            if (!StringUtils.isBlank(collectDTO.getOrderBy())) {
                if ("desc".equals(collectDTO.getSort())) {
                    queryWrapper.orderByDesc(collectDTO.getOrderBy());
                } else {
                    queryWrapper.orderByAsc(collectDTO.getOrderBy());
                }
            }
        }
        return queryWrapper;
    }

}